package com.pwc.conversionTool.csvProcess;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

import javax.servlet.ServletContext;

import com.pwc.conversionTool.controller.HomeController;

import au.com.bytecode.opencsv.CSVReader;

public class CsvProcess {

	
	HomeController controller;
//	private File fileName=new File("C:\\Users\\dwu102\\jboss-as-7.0.1.Final\\standalone\\tmp\\tmpFiles");
//	public String path = "C:\\Users\\dwu102\\jboss-as-7.0.1.Final\\standalone\\tmp\\tmpFiles";

	
	
	public CsvProcess(String path)
	{
		
	}
	
	public List<String> getTablehead() throws IOException {
		
		
//		File[] files = fileName.listFiles();
//		
//		fileName.getName();
//		
//		fileName.getPath();
		System.out.println("*************************************"+controller.context.getRealPath(""));
		
		List<String[]> list = null;
		System.out.print(controller.context.getRealPath(""));
		String filepath = controller.context.getRealPath("") + File.separator + "123.csv";
		

		CSVReader csvreader = null;

		csvreader = new CSVReader(new FileReader(filepath));
		list = csvreader.readAll();
		
		csvreader.close();
		if (list != null) {

			List<String> headList = Arrays.asList(list.get(0));
			return headList;

		}

		return null;

	}

	/**
	 * 
	 * @return content
	 */
	public List<List<String>> getTablecontent() {
		// List<List<String>> contentlist=new ArrayList<List<String>>();
		// List<List<String>> contentlist=null;
		List<String[]> list = null;
		String filepath = controller.context.getRealPath("")+ File.separator+ "123.csv";
		// File file = new File(path);
		CSVReader csvreader = null;
		try {
			csvreader = new CSVReader(new FileReader(filepath));
			list = csvreader.readAll();
			csvreader.close();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		
//		list.remove(0);
		
		if (list.get(0) != null && list.get(1) != null) {
			List<List<String>> contentlist = new ArrayList<List<String>>();
			// for(int i=1;list.get(i)!=null;i++)
			int i = 1;
			
//			for(String[] temp:list)
//			{
//				contentlist.add(Arrays.asList(temp));
//			}
			
			while (i<list.size()) {
				contentlist.add(Arrays.asList(list.get(i)));
				i++;
			}

			return contentlist;
		} else
			return null;
	}

}
